import os
import math
import random

dir_list = []
with open('directions.txt','r') as f:
    for item in f.readlines():
        t,o_r,o_z,o_th,tt = item.strip().split()
        dir_list.append((eval(o_r),eval(o_z),eval(o_th)))
print(dir_list)

r = 337.9
# z = 12.39
time = 1
def save_to_file(x,y,z,u,v,w,e,wt):
    global time
    with open('rst','a+') as f:
        z ='{:.15E}'.format(random.random() * 100).replace('E','D')
        f.write('   2.112000000000000D+03   {}   {}   {}   {}   {}   {}   {}   {}   {}   1.000000000000000D+01\n'.format(x,y,z,u,v,w,e,wt,'{:.15E}'.format(time).replace('E','D')))
    time += 1


energy_list = []
with open('energy.txt','r') as f:
    for item in f.readlines():
        a,b = item.strip().split()
        b = eval(b)
        energy_list.append(b)
print(len(energy_list))



file_list = []
for i in range(107):
    weight_list = []
    with open('AngularFlux/aflux_group{}'.format(i), 'r' ) as f :
        for i in range(1079):
            f.readline()
        f.readline()
        for i in range(12):
            w = eval(f.readline())
            if w < 0:
                print("to zero")
                w = 0

            weight_list.append(w)
    # print(weight_list)
    file_list.append(weight_list)

str_list = []

energy_list =[1.06770e+01]
z = -150 
end_z = 150 
start_du = math.radians(-25.4) 
end_du =  math.radians(25.4) 
step = math.radians(1.73)

while z < end_z:
# for z in range(-130,130,10):
    du = start_du
    while du < end_du:
        for group in range(len(energy_list)):
        
            x = r*math.cos(du)
            y = r*math.sin(du)
            for i in range(len(dir_list)):

                # miu = 1
                # eita = 1
                # w= 1
                miu = dir_list[i][0]
                eita = dir_list[i][2]
                w = dir_list[i][1]

                u = math.cos(du)*(miu + r) - math.sin(du) * eita - x
                v = math.sin(du) * (miu + r) +math.cos(du) * eita - y





                weight = file_list[group][i%12]
                str_list.append(('{:.15E}'.format(x).replace('E','D'),'{:.15E}'.format(y).replace('E','D'),'{:.15E}'.format(z).replace('E','D'),'{:.15E}'.format(u).replace('E','D'),'{:.15E}'.format(v).replace('E','D'),'{:.15E}'.format(w).replace('E','D'),'{:.15E}'.format(energy_list[group]).replace('E','D'),'{:.15E}'.format(weight).replace('E','D')))
                

        du += step

    z += 10


print(len(str_list[0]))


# for group in range(len(energy_list)):

#     x = r*math.cos(du)
#     y = r*math.sin(du)
#     for i in range(len(dir_list)):
#         miu = dir_list[i][0]
#         eita = dir_list[i][2]
#         w = dir_list[i][1]

#         u = math.cos(du)*(miu + r) - math.sin(du) * eita - x
#         v = math.sin(du) * (miu + r) +math.cos(du) * eita - y
#         w = -w 
#         u = -u
#         v = -v
#         weight = file_list[group][i]
#         str_list.append(('{:.15E}'.format(u).replace('E','D'),'{:.15E}'.format(v).replace('E','D'),'{:.15E}'.format(w).replace('E','D'),'{:.15E}'.format(energy_list[group]).replace('E','D'),'{:.15E}'.format(weight).replace('E','D')))

        # save_to_file('{:.15E}'.format(x).replace('E','D'),'{:.15E}'.format(y).replace('E','D'),'{:.15E}'.format(z).replace('E','D')
        #     ,'{:.15E}'.format(u).replace('E','D'),'{:.15E}'.format(v).replace('E','D'),'{:.15E}'.format(w).replace('E','D')
        #     ,'{:.15E}'.format(energy_list[group]).replace('E','D'),'{:.15E}'.format(weight).replace('E','D'))



str_list_length =len(str_list) 
print()
iii = 0



two_space = "  "
three_space = "   "
with open('rst', 'a+') as f:
    for t in range(len(str_list)):
        s_1=s_2=s_3=s_4=s_5=s_6=s_7=s_8=s_9=s_10=s_11=three_space
        s_5 = three_space
        
        first_line = '   2.112000000000000D+03'


        if str_list[t][0][0] == '-':
            s_2 = two_space        
        if str_list[t][1][0] == '-':
            s_3 = two_space        
        if str_list[t][2][0] == '-':
            s_4 = two_space        
        if str_list[t][3][0] == '-':
            s_5 = two_space        
        if str_list[t][4][0] == '-':
            s_6 = two_space  
        if str_list[t][5][0] == '-':
            s_7 = two_space               
        if str_list[t][6][0] == '-':
            s_8 = two_space  
        if str_list[t][7][0] == '-':
            s_9 = two_space  

        f.write('{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}\n'.format(first_line,s_2,str_list[t][0],s_3,str_list[t][1],s_4,str_list[t][2],s_5,str_list[t][3],s_6,str_list[t][4],s_7,str_list[t][5],s_8,str_list[t][6],s_9,str_list[t][7]))

        
        
        # f.write('{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}\n'.format(s_1,a,s_2,b,s_3,c,s_4,d,s_5,str_list[t][0],s_6,str_list[t][1],s_7,str_list[t][2],s_8,str_list[t][3],s_9,str_list[t][4])#,s_10,j,s_11,k))
        
        # t = iii % str_list_length
        # with open('rst','a+') as j:
        #     j.write('   {}   {}   {}   {}   {}   {}   {}   {}   {}   {}   {}\n'.format(a,b,c,d,str_list[t][0],str_list[t][1],str_list[t][2],str_list[t][3],str_list[t][4],j,k))

        # iii += 1




# t = i % str_list_length
#         a = dmp_list[i][0]
#         if '-' == a[0]:
#             s_1 = two_space
#         b = dmp_list[i][1]
#         if '-' == b[0]:
#             s_2 = two_space
#         c = dmp_list[i][2]
#         if '-' ==c[0]:
#             s_3 = two_space
#         d = dmp_list[i][3]
#         if '-' == d[0]:
#             s_4 = two_space
        
#         if str_list[t][0][0] == '-':
#             s_5 = two_space